package com.example.demo.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.example.demo.Common.UuidUtil;
import com.example.demo.VO.PermissionsVO;
import com.example.demo.annotation.AuthToken;
import com.example.demo.entity.Role_permissions;
import com.example.demo.entity.User;
import com.example.demo.service.IPermissionsService;
import com.example.demo.service.IRole_permissionsService;
import com.example.demo.service.IUserService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.xc.roadRisk.common.vo.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
* <p>
*  前端控制器
* </p>
*
* @author hanfei
* @since 2020-04-08
*/
@Api(value = "角色权限",tags = "角色权限")
@RestController
public class Role_permissionsController {
   @Autowired
   private IRole_permissionsService role_permissionsApi;
    @Autowired
    private IPermissionsService permissionsApi;
    @Autowired
    private IUserService userApi;
    private final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
    @ApiOperation(value = "分配权限", notes = "分配权限")
    @PostMapping(value = "/AssignPermissions")
    @AuthToken
    public Response<Boolean> AssignPermissions(@RequestParam(value = "roleid") String roleid,
                                               @RequestParam(value = "permissionsList") List<String> permissionsList) {
        Response response=new Response();
        role_permissionsApi.deletByRoleid(roleid);
        if (permissionsList != null) {
            List<Role_permissions> tdRolePermissionsList = new ArrayList<Role_permissions>();
            for (String permissionsid : permissionsList) {
                Role_permissions tdRolePermissions = new Role_permissions();
                String id=null;
                UuidUtil uuidUtil=new UuidUtil();
                if(uuidUtil.getId()!=null){
                    tdRolePermissions.setId(uuidUtil.getId());
                }
                tdRolePermissions.setRoleid(roleid);
                tdRolePermissions.setPermissionsid(permissionsid);
                tdRolePermissionsList.add(tdRolePermissions);
            }

          if(role_permissionsApi.insertBatch(tdRolePermissionsList)==true){
                response.setMsg("分配成功");
                response.setCode("200");
                return response;
          }else {
                response.setCode("500");
                response.setMsg("分配失败");
                return response;
          }

        }
        return response;
    }

    @ApiOperation(value = "根据角色id查找权限", notes = "根据角色id查找权限")
    @GetMapping(value = "/findPermissionsByRoleid")
    @AuthToken
    public Response findPermissionsByRoleid(String roleid) {
       Response response=new Response();
        List<PermissionsVO> rootMenu = role_permissionsApi.findByRoleid(roleid);
        List<PermissionsVO> menuList = new ArrayList<PermissionsVO>();
        for (int i = 0; i < rootMenu.size(); i++) {
            // 一级菜单没有parentId
            if (StringUtils.isBlank(rootMenu.get(i).getPid())) {
                menuList.add(rootMenu.get(i));
            }
        }
        // 为一级菜单设置子菜单，getChild是递归调用的
        for (PermissionsVO menu : menuList) {
            menu.setChildTDPermissions(permissionsApi.getChild(menu.getId(), rootMenu));
        }
        Map<String,Object> jsonMap = new HashMap<>();
        jsonMap.put("PermissionsList", menuList);
        response.setData(jsonMap);
        response.setCode("200");
        return response;
    }

    @ApiOperation(value = "根据用户查权限", notes = "根据用户查权限")
    @GetMapping(value = "/findPermissionsByUser")
    @AuthToken
    public Response findPermissionsByUser(HttpServletRequest request) {
        Response response=new Response();
        User user=(User)request.getSession().getAttribute("currentUser");
        User user1=userApi.selectOne(new EntityWrapper<>(user));
        if(user1.getRoleid()!=null){
            List<String> items = Arrays.asList(user1.getRoleid().split("\\s*,\\s*"));
            List<PermissionsVO> rootMenu=new ArrayList<>();
            if(items.size()>0){
              for (String s:items){
                  List<PermissionsVO> list=role_permissionsApi.findByRoleid(s);
                  rootMenu.addAll(list);
              }
            }
            List<PermissionsVO> menuList = new ArrayList<PermissionsVO>();
            for (int i = 0; i < rootMenu.size(); i++) {
                // 一级菜单没有parentId
                if (StringUtils.isBlank(rootMenu.get(i).getPid())) {
                    menuList.add(rootMenu.get(i));
                }
            }
            // 为一级菜单设置子菜单，getChild是递归调用的
            for (PermissionsVO menu : menuList) {
                menu.setChildTDPermissions(permissionsApi.getChild(menu.getId(), rootMenu));
            }
            Map<String,Object> jsonMap = new HashMap<>();
            jsonMap.put("PermissionsList", menuList);
            response.setData(jsonMap);
            response.setCode("200");

        }


        return response;
    }
}
